package com.baizhi.service.impl;

import com.baizhi.entity.PmsCategory;
import com.baizhi.dao.PmsCategoryMapper;
import com.baizhi.service.IPmsCategoryService;
import com.baizhi.vo.CascaderNodeVo;
import com.baizhi.vo.ZtreeNodeVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类; InnoDB free: 13312 kB 服务实现类
 * </p>
 *
 * @author lby
 * @since 2020-12-22
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements IPmsCategoryService {

    @Override
    public List<ZtreeNodeVo> getZTreeNodes() {
        List<PmsCategory> categories = this.list();
        List<ZtreeNodeVo> zTreeNodes = categories.stream()
                .filter(pmsCategory -> {
                    return pmsCategory.getCatLevel().equals(1);
                })
                .map(pmsCategory -> {
                    ZtreeNodeVo node = new ZtreeNodeVo();
                    node.setId(pmsCategory.getCatId());
                    node.setName(pmsCategory.getName());
//                    设置二级数据
                    node.setChildren(getChildrenNode(pmsCategory.getCatId(), categories));
                    return node;
                }).collect(Collectors.toList());
        return zTreeNodes;
    }

    @Override
    public List<CascaderNodeVo> getCascaderNodeVos() {
        List<PmsCategory> categories = this.list();
        return categories.stream()
                .filter(pmsCategory -> {
                    return pmsCategory.getCatLevel() == 1;
                }).map(pmsCategory -> {
                    CascaderNodeVo vo = new CascaderNodeVo();
                    vo.setValue(pmsCategory.getCatId());
                    vo.setLabel(pmsCategory.getName());
                    vo.setChildren(getCascaderChildrenNode(pmsCategory.getCatId(),categories));
                    return vo;
                }).collect(Collectors.toList());
    }

    private List<CascaderNodeVo> getCascaderChildrenNode(Long catId, List<PmsCategory> categories) {
        return categories.stream().filter(pmsCategory -> {
            return pmsCategory.getParentCid().equals(catId);
        }).map(pmsCategory -> {
            CascaderNodeVo vo = new CascaderNodeVo();
            vo.setValue(pmsCategory.getCatId());
            vo.setLabel(pmsCategory.getName());
            vo.setChildren(getCascaderChildrenNode(pmsCategory.getCatId(),categories));
            return vo;
        }).collect(Collectors.toList());
    }

    private List<ZtreeNodeVo> getChildrenNode(Long catId, List<PmsCategory> categories) {
        List<ZtreeNodeVo> zTreeNodes = categories.stream()
                .filter(pmsCategory -> {
                    return pmsCategory.getParentCid().equals(catId);
                }).map(pmsCategory -> {
                    ZtreeNodeVo node = new ZtreeNodeVo();
                    node.setId(pmsCategory.getCatId());
                    node.setName(pmsCategory.getName());
//                    设置二级数据
                    node.setChildren(getChildrenNode(pmsCategory.getCatId(), categories));
                    return node;
                }).collect(Collectors.toList());
        return zTreeNodes;
    }
}
